#include "QtAsioThread.h" 
#include <QTimer>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

namespace vkbrd
{
	void QtAsioThread::pollLoop()
	{
		if (!terminating)
		{
			comClient->poll();
			QTimer::singleShot(5, this, SLOT(pollLoop())); ///@todo Poll time change
		}
	}

	void QtAsioThread::writeToPort( unsigned char msg )
	{
		ioService.post(boost::bind(&miniComClient::do_write, comClient, msg));
	}

	void QtAsioThread::stop()
	{
		terminating = true;

		comClient->do_close(boost::system::error_code());
		ioService.stop();
		delete comClient;
		exit();
	}

	QtAsioThread::QtAsioThread(int _baudRate, std::string& _portName)
	{
		terminating = false;
		comClient = new miniComClient(ioService, _baudRate, _portName);
		bool success = connect(comClient, SIGNAL(receivedData(unsigned char*, int)), this, SIGNAL(receivedData(unsigned char*, int)));
		assert(success);
	}
}

